home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xvmahjon.por / xvmahjon / xvmahjongg / sunview / mahjongg.c < prev    next >
C/C++ Source or Header  |  1991-12-20  |  26KB  |  1,110 lines

  1. /*
  2.  *    Copyright 1988, Mark Holm
  3.  *            Exceptions
  4.  *
  5.  *    Acknowledgments to Dorothy Robinson for her artistic
  6.  *     abilities in drawing the icons and to Jim Batch for
  7.  *     technical support and graphical concepts (which I abandoned in favor
  8.  *       of the easy way out).
  9.  *
  10.  *    Permission is given to copy and distribute for non-profit purposes.
  11.  *
  12.  *    Revision History
  13.  *    ----------------
  14.  *
  15.  *    1.0 Initial release
  16.  *
  17.  *    1.1 removed non-working reverse video cursor code
  18.  *        added blank tiles for hidden tiles to stop cheating.
  19.  *
  20.  *    1.2 incorporated sun fix for panel messages and added new
  21.  *         shuffling algorithm.
  22.  */
  23.  
  24. #ifndef lint
  25. static char *rcs = "$header$ Copyright 1988 Mark Holm";
  26. #endif !lint
  27.  
  28. #include <stdio.h>
  29. #include <sys/types.h>
  30. #include <sys/time.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/file.h>
  33. #include <sun/fbio.h>
  34. #include <suntool/sunview.h>
  35. #include <suntool/panel.h>
  36. #include <suntool/canvas.h>
  37. #include <suntool/icon.h>
  38. #include <sunwindow/notify.h>
  39. #include <pixrect/pixrect.h>
  40.  
  41. #include "mahjongg.h"
  42.  
  43. void            die();
  44. void            build_image();
  45. void            place_tiles();
  46. Pixrect            *color_button();
  47.  
  48. #ifdef RELEASE_1
  49. /* Black and white closed icon image */
  50.  
  51. static short        icon_image[] = {
  52. #include "mahjongg.icon"
  53. };
  54. #else
  55. extern short icon_image[];
  56. #endif
  57. DEFINE_ICON_FROM_IMAGE(icon, icon_image); /* Black and white icon */
  58. struct icon    *cicon;              /* storage for color icon */
  59.  
  60. /* externals */
  61.  
  62. extern void        quit_proc();
  63. extern void        undo_proc();
  64. extern void        new_proc();
  65. extern void        again_proc();
  66. extern void        help_proc();
  67. extern void        board_num_proc();
  68. extern void        play_back_proc();
  69. extern void        play_event_proc();
  70. extern void        panel_msg();
  71.  
  72. extern short        stick_image[];
  73. extern int        undo_count;
  74.  
  75. /* overlap globals */
  76.  
  77.  
  78. Frame            main_frame;
  79. Panel            play_panel, message_panel;
  80. Panel_item        TL_hundred;
  81. Panel_item        TL_ten;
  82. Panel_item        TL_one;
  83. Panel_item        message;
  84. Panel_item        tile[144];
  85. Panel_item        tiles_left[3];
  86. Cursor            play_cursor;
  87. boolean            BandW = FALSE;
  88. Tiles            *board[144];
  89. Selected        selected[2];
  90. Selected        last_item;
  91. int            tile_count;
  92. char            state[256];
  93.  
  94. /* local globals */
  95.  
  96. Pixwin            *frame_pw;
  97. Panel_item        tile_message;
  98. Panel_item        cp1;
  99. Panel_item        cp2;
  100. Panel_item        cp3;
  101. Panel_item        cp4;
  102. Panel_item        help;
  103. Panel_item        again;
  104. Panel_item        new;
  105. Panel_item        quit;
  106. Panel_item        undo;
  107. Panel_item        board_num;
  108. struct timeval        *tp;
  109. struct timezone        *tz;
  110. int            seed;
  111.  
  112. /* define color map */
  113.  
  114. #include "color.h"
  115.  
  116. main(argc, argv)
  117. int argc;
  118. char **argv;
  119. {
  120.     struct pixfont    *panel_font;
  121.     struct pixfont    *dummy_font;
  122.        int         i;
  123.        int         middle;
  124.     struct fbtype     fb;
  125.        int         open_stat;
  126.  
  127.     /* determine type of frame buffer and set BandW accordingly */
  128.  
  129.     open_stat = open("/dev/fb", O_RDONLY);
  130.     (void) ioctl(open_stat, FBIOGTYPE, &fb);
  131.  
  132.     if ( (fb.fb_type == FBTYPE_SUN1BW) ||
  133.      (fb.fb_type == FBTYPE_SUN2BW) )
  134.         BandW = TRUE;
  135.  
  136.     /* initialize random number generator seed */
  137.  
  138.     tp = (struct timeval *) malloc(sizeof(struct timeval));
  139.     tz = (struct timezone *) malloc(sizeof(struct timezone));
  140.     gettimeofday(tp, tz);
  141.     (void) initstate((unsigned) (tp->tv_sec % 255), state, 256); /* initialize random state array */
  142.     seed = RANDOM(20011);
  143.     free(tp);
  144.     free(tz);
  145.  
  146.     /* create main frame */
  147.    
  148.     main_frame = window_create(NULL, FRAME,
  149.                    FRAME_SHOW_LABEL, FALSE,
  150.                    FRAME_SUBWINDOWS_ADJUSTABLE, FALSE,
  151.                    FRAME_ICON, &icon,
  152.                    FRAME_ARGC_PTR_ARGV, &argc, argv,
  153.                    WIN_HEIGHT, FRAME_Y_MAX,
  154.                    WIN_WIDTH, FRAME_X_MAX,
  155.                    0);
  156.  
  157.     /* parse arguments */
  158.     for(argc--, argv++; argc > 0; argc--, argv++)
  159.     if (argv[0][0] = '-')
  160.         switch (argv[0][1]) {
  161.         case 'c': /* force color */
  162.         BandW = FALSE;
  163.         break;
  164.         case 'b': /* force black and white */
  165.         BandW = TRUE;
  166.         break;
  167.         case 'n': /* set board number */
  168.         if(argc-- == 0)
  169.             die("Usage: mahjongg [-b] [-c] [-n #]\n", 0);
  170.         argv++;
  171.             sscanf(argv[0] , "%d", &seed);
  172.         if (seed > 20011) {
  173.             printf("Board numbers must be < 20011");
  174.             seed %= 20011;
  175.         }
  176.         break;
  177.         default:
  178.         die("Usage: mahjongg [-b] [-c] [-n #]\n", 0);
  179.         break;
  180.         }
  181.     else
  182.         die("Usage: mahjongg [-b] [-c] [-n #]\n", 0);
  183.         
  184.     /* if color then apply color icon to window icon */
  185.  
  186.     if(!BandW) {
  187.     cicon = (struct icon *) window_get(main_frame, FRAME_ICON);
  188.     cicon->ic_mpr = &cicon_image;
  189.      }
  190.  
  191.     /* get pixwin to apply color maps */
  192.  
  193.     frame_pw  = (Pixwin *) window_get(main_frame, WIN_PIXWIN, 0);
  194.  
  195.     /* apply color maps to frame pixwin */
  196.  
  197.     pw_setcmsname(frame_pw, "mahjongg");
  198.     pw_putcolormap(frame_pw, 0, MAX_COLORS+1, red, green, blue);
  199.     if (BandW)
  200.         pw_putcolormap(frame_pw, 0, 1, &(red[WHITE]), &(green[WHITE]), &(blue[WHITE]));
  201.  
  202.     /* set inheritable colors */
  203.  
  204.     window_set(main_frame, FRAME_INHERIT_COLORS, TRUE, 0);
  205.  
  206.     /* set up the panel on the right hand side */
  207.     
  208.     dummy_font = pf_open("/usr/lib/fonts/fixedwidthfonts/screen.r.7");
  209.     panel_font = pf_open("/usr/lib/fonts/fixedwidthfonts/screen.b.14");
  210.     message_panel = window_create(main_frame, PANEL,
  211.               WIN_HEIGHT, MESS_Y_MAX,
  212.               WIN_WIDTH, MESS_X_MAX,
  213.               WIN_X, 0,
  214.               WIN_Y, 0,
  215.               WIN_FONT, dummy_font,
  216.               0);
  217.  
  218.     /* determine middle of panel */
  219.  
  220.     middle = (MESS_X_MAX / 2);
  221.  
  222.     /* create tile counters */
  223.  
  224.     TL_hundred = panel_create_item(message_panel, PANEL_MESSAGE,
  225.                  PANEL_LABEL_IMAGE,
  226.                    (BandW) ? &NUM1 : &cNUM1,
  227.                  PANEL_ITEM_Y, ATTR_ROW(7),
  228.                  PANEL_ITEM_X, X_LOC,
  229.                  0);
  230.     TL_ten = panel_create_item(message_panel, PANEL_MESSAGE,
  231.                  PANEL_LABEL_IMAGE,
  232.                    (BandW) ? &NUM4 : &cNUM4,
  233.                  PANEL_ITEM_Y, ATTR_ROW(7),
  234.                  PANEL_ITEM_X, X_LOC +  W_BASE_TILE,
  235.                  0);
  236.     TL_one = panel_create_item(message_panel, PANEL_MESSAGE,
  237.                  PANEL_LABEL_IMAGE,
  238.                    (BandW) ? &NUM4 : &cNUM4,
  239.                  PANEL_ITEM_Y, ATTR_ROW(7),
  240.                  PANEL_ITEM_X, X_LOC + (W_BASE_TILE * 2),
  241.                  0);
  242.  
  243.     /* create game label messages */
  244.  
  245.     cp1 = panel_create_item(message_panel, PANEL_MESSAGE,
  246.                  PANEL_LABEL_FONT, panel_font,
  247.                  PANEL_LABEL_STRING, "MAHJONGG",
  248.                  PANEL_ITEM_Y, ATTR_ROW(1) - 11,
  249.                  PANEL_ITEM_X, middle - 65,
  250.                  0);
  251.     cp2 = panel_create_item(message_panel, PANEL_MESSAGE,
  252.                  PANEL_LABEL_FONT, dummy_font,
  253.                  PANEL_LABEL_STRING, "Copyright 1988",
  254.                  PANEL_ITEM_Y, ATTR_ROW(2),
  255.                  PANEL_ITEM_X, middle - 70,
  256.                  0);
  257.     cp3 = panel_create_item(message_panel, PANEL_MESSAGE,
  258.                  PANEL_LABEL_FONT, dummy_font,
  259.                  PANEL_LABEL_STRING, "Mark A. Holm",
  260.                  PANEL_ITEM_Y, ATTR_ROW(3),
  261.                  PANEL_ITEM_X, middle - 65,
  262.                  0);
  263.     cp3 = panel_create_item(message_panel, PANEL_MESSAGE,
  264.                  PANEL_LABEL_FONT, dummy_font,
  265.                  PANEL_LABEL_STRING, "Exceptions",
  266.                  PANEL_ITEM_Y, ATTR_ROW(4),
  267.                  PANEL_ITEM_X, middle - 60,
  268.                  0);
  269.     tile_message = panel_create_item(message_panel, PANEL_MESSAGE,
  270.                                      PANEL_LABEL_FONT, panel_font,
  271.                                      PANEL_LABEL_STRING, "Tiles Remaining",
  272.                                      PANEL_ITEM_Y, ATTR_ROW(5),
  273.                                      PANEL_ITEM_X,  X_LOC + 20,
  274.                                      0);
  275.  
  276.     /* create seed item */
  277.  
  278.     board_num = panel_create_item(message_panel, PANEL_TEXT,
  279.                                      PANEL_LABEL_FONT, panel_font,
  280.                                      PANEL_VALUE_FONT, panel_font,
  281.                                      PANEL_LABEL_STRING, "Board Number : ",
  282.                      PANEL_VALUE, "",
  283.                                      PANEL_ITEM_Y, ATTR_ROW(6),
  284.                                      PANEL_ITEM_X,  middle,
  285.                      PANEL_NOTIFY_PROC, board_num_proc,
  286.                                      0);
  287.  
  288.     /* create control buttons */
  289.  
  290.     help = panel_create_item(message_panel, PANEL_BUTTON,
  291.                  PANEL_ITEM_Y, ATTR_ROW(8),
  292.                  PANEL_ITEM_X, middle,
  293.                                  PANEL_LABEL_IMAGE,
  294.                                    color_button(panel_button_image(message_panel,
  295.                                   "HELP",
  296.                                   6,
  297.                                                            panel_font),
  298.                         CYAN),
  299.                  PANEL_NOTIFY_PROC, help_proc,
  300.                  0);
  301.  
  302.     again = panel_create_item(message_panel, PANEL_BUTTON,
  303.                                  PANEL_LABEL_IMAGE,
  304.                                    color_button(panel_button_image(message_panel,
  305.                                   "AGAIN",
  306.                                   6,
  307.                                                            panel_font),
  308.                         YELLOW),
  309.                  PANEL_NOTIFY_PROC, again_proc,
  310.                  0);
  311.  
  312.     new = panel_create_item(message_panel, PANEL_BUTTON,
  313.                                  PANEL_LABEL_IMAGE,
  314.                                    color_button(panel_button_image(message_panel,
  315.                                   "NEW",
  316.                                   6,
  317.                                                            panel_font),
  318.                         GREEN),
  319.                  PANEL_NOTIFY_PROC, new_proc,
  320.                  0);
  321.  
  322.     undo = panel_create_item(message_panel, PANEL_BUTTON,
  323.                                  PANEL_LABEL_IMAGE,
  324.                                    color_button(panel_button_image(message_panel,
  325.                                   "UNDO",
  326.                                   6,
  327.                                                            panel_font),
  328.                         MAGENTA),
  329.                  PANEL_NOTIFY_PROC, undo_proc,
  330.                  PANEL_SHOW_ITEM, TRUE,
  331.                  0);
  332.  
  333.     quit = panel_create_item(message_panel, PANEL_BUTTON,
  334.                                  PANEL_LABEL_IMAGE,
  335.                                    color_button(panel_button_image(message_panel,
  336.                                   "QUIT",
  337.                                   6,
  338.                                                            panel_font),
  339.                         RED),
  340.                  PANEL_NOTIFY_PROC, quit_proc,
  341.                  0);
  342.  
  343.     /* place conceled message */
  344.  
  345.     message = panel_create_item(message_panel, PANEL_MESSAGE,
  346.                                  PANEL_LABEL_FONT, panel_font,
  347.                                  PANEL_ITEM_Y, ATTR_ROW(10),
  348.                  PANEL_ITEM_X, middle,
  349.                  PANEL_LABEL_STRING, "",
  350.                  PANEL_SHOW_ITEM, FALSE,
  351.                  0);
  352.  
  353.      /* create cursor for play panel*/
  354.  
  355.     play_cursor = cursor_create(CURSOR_IMAGE, &stick,
  356.                 CURSOR_XHOT, 8,
  357.                 CURSOR_YHOT, 8,
  358.                 0);
  359.  
  360.     if (!BandW) {
  361.  
  362.     cursor_set(play_cursor, CURSOR_OP,
  363.                  (PIX_SRC^PIX_DST) | PIX_COLOR( YELLOW ),
  364.                 0);
  365.      }
  366.  
  367.     /* set up panel for the play */
  368.     
  369.     play_panel = window_create(main_frame, PANEL,
  370.                WIN_CONSUME_PICK_EVENTS,
  371.                 WIN_NO_EVENTS, WIN_MOUSE_BUTTONS, WIN_UP_EVENTS, WIN_LEFT_KEYS, 0,
  372.                WIN_HEIGHT, PLAY_Y_MAX,
  373.                WIN_WIDTH, PLAY_X_MAX,
  374.                WIN_X, 0,
  375.                WIN_Y, MESS_Y_MAX + BORDER,
  376.                WIN_CURSOR, play_cursor,
  377.                PANEL_PAINT, PANEL_NONE,
  378.                PANEL_BACKGROUND_PROC, play_back_proc,
  379.                PANEL_EVENT_PROC, play_event_proc,
  380.                CANVAS_RETAINED, TRUE,
  381.                0);
  382.  
  383.     window_set(message_panel, WIN_INPUT_DESIGNEE,
  384.                window_get(play_panel, WIN_DEVICE_NUMBER),
  385.                0);
  386.  
  387.     /* build board image */
  388.  
  389.     build_image(FALSE);
  390.     place_tiles(TRUE);
  391.  
  392.     /* start main processing */
  393.               
  394.     window_main_loop(main_frame);
  395.     exit(0);
  396. }
  397.  
  398.  
  399. /* die because of some UNIX error */
  400. void die(message, pperr)
  401. char *message;
  402. int pperr;
  403. {
  404.     fprintf(stderr, message);
  405.     if (pperr)
  406.     perror("mahjongg");
  407.     exit(1);
  408. }
  409.  
  410. void place_tiles(newboard)
  411. boolean        newboard;
  412.  
  413. {
  414.     int     i;
  415.     int     j;
  416.     int     k;
  417.     int     x_loc;
  418.     int     y_loc;
  419.  
  420.     /* check if not new and destroy existing panel buttons */
  421.  
  422.     if (!newboard)
  423.     for(i = 0; i < 144; i++)
  424.         panel_destroy_item(tile[i]);
  425.  
  426.     /* place tiles */
  427.  
  428.         /* row 1 level 1 */
  429.  
  430.     for(i = 0,
  431.         x_loc = COL2;
  432.         i < 12;
  433.         i++,
  434.         x_loc += W_BASE_TILE) 
  435.  
  436.             tile[i] = panel_create_item(play_panel, PANEL_BUTTON,
  437.                         PANEL_LABEL_IMAGE,
  438.                           (board[i]->top_free) ?
  439.                             board[i]->image :
  440.                               (BandW) ? &BLANK : &cBLANK,
  441.                         PANEL_ITEM_X,
  442.                          x_loc,
  443.                          PANEL_ITEM_Y,
  444.                          ROW1,
  445.                         PANEL_SHOW_ITEM,
  446.                          TRUE,
  447.                         PANEL_CLIENT_DATA,
  448.                          (caddr_t) board[i],
  449.                          0);
  450.  
  451.     /* row 2 level 1 */
  452.  
  453.     for(x_loc = COL4,
  454.         j = 0;
  455.         j < 8;
  456.         j++,
  457.         i++,
  458.         x_loc += W_BASE_TILE) 
  459.  
  460.             tile[i] = panel_create_item(play_panel, PANEL_BUTTON,
  461.                         PANEL_LABEL_IMAGE,
  462.                           (board[i]->top_free) ?
  463.                             board[i]->image :
  464.                               (BandW) ? &BLANK : &cBLANK,
  465.                         PANEL_ITEM_X,
  466.                          x_loc,
  467.                          PANEL_ITEM_Y,
  468.                          ROW2,
  469.                         PANEL_SHOW_ITEM,
  470.                          TRUE,
  471.                         PANEL_CLIENT_DATA,
  472.                          (caddr_t) board[i],
  473.                          0);
  474.  
  475.     /* row 3 level 1 */
  476.  
  477.     for(x_loc = COL3,
  478.         j = 0;
  479.         j < 10;
  480.         j++,
  481.         i++,
  482.         x_loc += W_BASE_TILE) 
  483.  
  484.             tile[i] = panel_create_item(play_panel, PANEL_BUTTON,
  485.                         PANEL_LABEL_IMAGE,
  486.                           (board[i]->top_free) ?
  487.                             board[i]->image :
  488.                               (BandW) ? &BLANK : &cBLANK,
  489.                         PANEL_ITEM_X,
  490.                          x_loc,
  491.                          PANEL_ITEM_Y,
  492.                          ROW3,
  493.                         PANEL_SHOW_ITEM,
  494.                          TRUE,
  495.                         PANEL_CLIENT_DATA,
  496.                          (caddr_t) board[i],
  497.                          0);
  498.  
  499.     /* row 4 1/2 level 1 */
  500.  
  501.     /* Left */
  502.  
  503.             tile[i] = panel_create_item(play_panel, PANEL_BUTTON,
  504.                         PANEL_LABEL_IMAGE,
  505.                           (board[i]->top_free) ?
  506.                             board[i]->image :
  507.                               (BandW) ? &BLANK : &cBLANK,
  508.                         PANEL_ITEM_X,
  509.                          COL1,
  510.                          PANEL_ITEM_Y,
  511.                          ROW4pt5,
  512.                         PANEL_SHOW_ITEM,
  513.                          TRUE,
  514.                         PANEL_CLIENT_DATA,
  515.                          (caddr_t) board[i],
  516.                          0);
  517.  
  518.         i++; /* increment tile counter */
  519.  
  520.     /* row 4 level 1 */
  521.  
  522.     for(x_loc = COL2,
  523.         j = 0;
  524.         j < 12;
  525.         j++,
  526.         i++,
  527.         x_loc += W_BASE_TILE) 
  528.  
  529.             tile[i] = panel_create_item(play_panel, PANEL_BUTTON,
  530.                         PANEL_LABEL_IMAGE,
  531.                           (board[i]->top_free) ?
  532.                             board[i]->image :
  533.                               (BandW) ? &BLANK : &cBLANK,
  534.                         PANEL_ITEM_X,
  535.                          x_loc,
  536.                          PANEL_ITEM_Y,
  537.                          ROW4,
  538.                         PANEL_SHOW_ITEM,
  539.                          TRUE,
  540.                         PANEL_CLIENT_DATA,
  541.                          (caddr_t) board[i],
  542.                          0);
  543.  
  544.     /* row 5 level 1 */
  545.  
  546.     for(x_loc = COL2,
  547.         j = 0;
  548.         j < 12;
  549.         j++,
  550.         i++,
  551.         x_loc += W_BASE_TILE) 
  552.  
  553.             tile[i] = panel_create_item(play_panel, PANEL_BUTTON,
  554.                         PANEL_LABEL_IMAGE,
  555.                           (board[i]->top_free) ?
  556.                             board[i]->image :
  557.                               (BandW) ? &BLANK : &cBLANK,
  558.                         PANEL_ITEM_X,
  559.                          x_loc,
  560.                          PANEL_ITEM_Y,
  561.                          ROW5,
  562.                         PANEL_SHOW_ITEM,
  563.                          TRUE,
  564.                         PANEL_CLIENT_DATA,
  565.                          (caddr_t) board[i],
  566.                          0);
  567.     /* row 4 1/2 level 1 */
  568.  
  569.     /* Right */
  570.  
  571.             tile[i] = panel_create_item(play_panel, PANEL_BUTTON,
  572.                         PANEL_LABEL_IMAGE,
  573.                           (board[i]->top_free) ?
  574.                             board[i]->image :
  575.                               (BandW) ? &BLANK : &cBLANK,
  576.                         PANEL_ITEM_X,
  577.                          COL14,
  578.                          PANEL_ITEM_Y,
  579.                          ROW4pt5,
  580.                         PANEL_SHOW_ITEM,
  581.                          TRUE,
  582.                         PANEL_CLIENT_DATA,
  583.                          (caddr_t) board[i],
  584.                          0);
  585.  
  586.         i++; /* increment tile counter */
  587.  
  588.             tile[i] = panel_create_item(play_panel, PANEL_BUTTON,
  589.                         PANEL_LABEL_IMAGE,
  590.                           (board[i]->top_free) ?
  591.                             board[i]->image :
  592.                               (BandW) ? &BLANK : &cBLANK,
  593.                         PANEL_ITEM_X,
  594.                          COL15,
  595.                          PANEL_ITEM_Y,
  596.                          ROW4pt5,
  597.                         PANEL_SHOW_ITEM,
  598.                          TRUE,
  599.                         PANEL_CLIENT_DATA,
  600.                          (caddr_t) board[i],
  601.                          0);
  602.  
  603.         i++; /* increment tile counter */
  604.  
  605.     /* row 6 level 1 */
  606.  
  607.     for(x_loc = COL3,
  608.         j = 0;
  609.         j < 10;
  610.         j++,
  611.         i++,
  612.         x_loc += W_BASE_TILE) 
  613.  
  614.             tile[i] = panel_create_item(play_panel, PANEL_BUTTON,
  615.                         PANEL_LABEL_IMAGE,
  616.                           (board[i]->top_free) ?
  617.                             board[i]->image :
  618.                               (BandW) ? &BLANK : &cBLANK,
  619.                         PANEL_ITEM_X,
  620.                          x_loc,
  621.                          PANEL_ITEM_Y,
  622.                          ROW6,
  623.                         PANEL_SHOW_ITEM,
  624.                          TRUE,
  625.                         PANEL_CLIENT_DATA,
  626.                          (caddr_t) board[i],
  627.                          0);
  628.  
  629.     /* row 7 level 1 */
  630.  
  631.     for(x_loc = COL4,
  632.         j = 0;
  633.         j < 8;
  634.         j++,
  635.         i++,
  636.         x_loc += W_BASE_TILE) 
  637.  
  638.             tile[i] = panel_create_item(play_panel, PANEL_BUTTON,
  639.                         PANEL_LABEL_IMAGE,
  640.                           (board[i]->top_free) ?
  641.                             board[i]->image :
  642.                               (BandW) ? &BLANK : &cBLANK,
  643.                         PANEL_ITEM_X,
  644.                          x_loc,
  645.                          PANEL_ITEM_Y,
  646.                          ROW7,
  647.                         PANEL_SHOW_ITEM,
  648.                          TRUE,
  649.                         PANEL_CLIENT_DATA,
  650.                          (caddr_t) board[i],
  651.                          0);
  652.  
  653.         /* row 8 level 1 */
  654.  
  655.     for(j = 0,
  656.         x_loc = COL2;
  657.         j < 12;
  658.         j++,
  659.         i++,
  660.         x_loc += W_BASE_TILE) 
  661.  
  662.             tile[i] = panel_create_item(play_panel, PANEL_BUTTON,
  663.                         PANEL_LABEL_IMAGE,
  664.                           (board[i]->top_free) ?
  665.                             board[i]->image :
  666.                               (BandW) ? &BLANK : &cBLANK,
  667.                         PANEL_ITEM_X,
  668.                          x_loc,
  669.                          PANEL_ITEM_Y,
  670.                          ROW8,
  671.                         PANEL_SHOW_ITEM,
  672.                          TRUE,
  673.                         PANEL_CLIENT_DATA,
  674.                          (caddr_t) board[i],
  675.                          0);
  676.  
  677.     /* rows 1-6 level 2 */
  678.  
  679.     for(y_loc = ROW2 - B_TILE_SHADOW,
  680.         j = 0;
  681.         j < 6;
  682.         j++,
  683.         y_loc += H_BASE_TILE) 
  684.  
  685.         for(x_loc = COL5 - S_TILE_SHADOW,
  686.             k = 0;
  687.             k < 6;
  688.             i++,
  689.             k++,
  690.             x_loc += W_BASE_TILE) 
  691.  
  692.                 tile[i] = panel_create_item(play_panel, PANEL_BUTTON,
  693.                             PANEL_LABEL_IMAGE,
  694.                               (board[i]->top_free) ?
  695.                                 board[i]->image :
  696.                                   (BandW) ? &BLANK : &cBLANK,
  697.                             PANEL_ITEM_X,
  698.                              x_loc,
  699.                              PANEL_ITEM_Y,
  700.                              y_loc,
  701.                             PANEL_SHOW_ITEM,
  702.                              TRUE,
  703.                             PANEL_CLIENT_DATA,
  704.                              (caddr_t) board[i],
  705.                              0);
  706.  
  707.     /* rows 1-4 level 3 */
  708.  
  709.     for(y_loc = ROW3 - (B_TILE_SHADOW * 2),
  710.         j = 0;
  711.         j < 4;
  712.         j++,
  713.         y_loc += H_BASE_TILE) 
  714.  
  715.         for(x_loc = COL6 - (S_TILE_SHADOW * 2),
  716.             k = 0;
  717.             k < 4;
  718.             i++,
  719.             k++,
  720.             x_loc += W_BASE_TILE) 
  721.  
  722.                 tile[i] = panel_create_item(play_panel, PANEL_BUTTON,
  723.                             PANEL_LABEL_IMAGE,
  724.                               (board[i]->top_free) ?
  725.                                 board[i]->image :
  726.                                   (BandW) ? &BLANK : &cBLANK,
  727.                             PANEL_ITEM_X,
  728.                              x_loc,
  729.                              PANEL_ITEM_Y,
  730.                              y_loc,
  731.                             PANEL_SHOW_ITEM,
  732.                              TRUE,
  733.                             PANEL_CLIENT_DATA,
  734.                              (caddr_t) board[i],
  735.                              0);
  736.  
  737.     /* rows 1-2 level 4 */
  738.  
  739.     for(y_loc = ROW4 - (B_TILE_SHADOW * 3),
  740.         j = 0;
  741.         j < 2;
  742.         j++,
  743.         y_loc += H_BASE_TILE) 
  744.  
  745.         for(x_loc = COL7 - (S_TILE_SHADOW * 3),
  746.             k = 0;
  747.             k < 2;
  748.             i++,
  749.             k++,
  750.             x_loc += W_BASE_TILE) 
  751.  
  752.                 tile[i] = panel_create_item(play_panel, PANEL_BUTTON,
  753.                             PANEL_LABEL_IMAGE,
  754.                               board[i]->image,
  755.                             PANEL_ITEM_X,
  756.                              x_loc,
  757.                              PANEL_ITEM_Y,
  758.                              y_loc,
  759.                             PANEL_SHOW_ITEM,
  760.                              TRUE,
  761.                             PANEL_CLIENT_DATA,
  762.                              (caddr_t) board[i],
  763.                              0);
  764.  
  765.     /* Cap tile */
  766.  
  767.             tile[i] = panel_create_item(play_panel, PANEL_BUTTON,
  768.                         PANEL_LABEL_IMAGE,
  769.                           board[i]->image,
  770.                         PANEL_ITEM_X,
  771.                          COL7pt5 - (S_TILE_SHADOW * 4),
  772.                          PANEL_ITEM_Y,
  773.                          ROW4pt5 - (B_TILE_SHADOW * 4),
  774.                         PANEL_SHOW_ITEM,
  775.                          TRUE,
  776.                         PANEL_CLIENT_DATA,
  777.                          (caddr_t) board[i],
  778.                          0);
  779.  
  780.     /* paint panel */
  781.  
  782.     panel_paint(play_panel, PANEL_NO_CLEAR);
  783.  
  784.    /* clear stand_by message  and release input mask */
  785.  
  786.    panel_msg((char *)0, &stick);
  787.  
  788. }
  789.  
  790. void build_image(oldimage)
  791. boolean oldimage;
  792.  
  793. {
  794. int     i;
  795. int     j;
  796. boolean ok;
  797. boolean dir;
  798. char    seed_text[80];
  799. int   tmp_pool[144];
  800. int   tmp_tiles_left;
  801. int   pos;
  802.  
  803.     /* initialize selected structures */
  804.  
  805.         selected[0].filled = FALSE;
  806.         selected[1].filled = FALSE;
  807.         last_item.filled   = FALSE;
  808.         undo_count          = -1;
  809.  
  810.         panel_set(message,   PANEL_SHOW_ITEM,
  811.                    FALSE,
  812.                   0);
  813.         panel_set(TL_hundred, PANEL_LABEL_IMAGE,
  814.                    (BandW) ? &NUM1 : &cNUM1,
  815.                   PANEL_SHOW_ITEM,
  816.                    TRUE,
  817.                   0);
  818.         panel_set(TL_ten    , PANEL_LABEL_IMAGE,
  819.                    (BandW) ? &NUM4 : &cNUM4,
  820.                   PANEL_SHOW_ITEM,
  821.                    TRUE,
  822.                   0);
  823.         panel_set(TL_one    , PANEL_LABEL_IMAGE,
  824.                    (BandW) ? &NUM4 : &cNUM4,
  825.                   0);
  826.  
  827.     /* display current seed in text item */
  828.  
  829.         sprintf(seed_text, "%d", seed);
  830.         panel_set(board_num, PANEL_VALUE, seed_text, 0);
  831.  
  832.     /* show stand_by message while building image  and grab all input */
  833.  
  834.     panel_msg( "Building board. Please wait.", &waiting);
  835.  
  836.     /* initialize random number counter */
  837.  
  838.     (void) srandom(seed);
  839.  
  840.     tile_count = 144;
  841.  
  842.     /* initialize  tmp_pool */
  843.         for(j = 0; j < 136; j++) tmp_pool[j] = j/4; /* tiles * 4 */
  844.         for(j=136; j<144; j++) tmp_pool[j] = j- 102; /* flowers & seasons */
  845.                                                    /* 136 --> 34 */
  846.                                                    /* 143 --> 41 */
  847.         tmp_tiles_left = 144;
  848.  
  849.  
  850.  
  851.     /* assign values to each location. Board is built from upper left *
  852.      * to lower right, bottom to top. Exception are left tile for row *
  853.      * 4pt5 is before rows 4 and 5, and right tiles for row 4.5 are   *
  854.      * after rows 4 and 5                                             */
  855.  
  856.     for(j = 0; j < 144; j++) {
  857.         if (board[j] == NULL) /* intialize array */
  858.         board[j] = (Tiles *) malloc(sizeof(Tiles));
  859.  
  860.         if (!oldimage) { /* not repeating last board */
  861.  
  862.            /* Shuffle/pick tile */
  863.  
  864.                pos = RANDOM(tmp_tiles_left);
  865.                i= tmp_pool[pos];
  866.                for(;pos<tmp_tiles_left; pos++) tmp_pool[pos] = tmp_pool[pos+1];
  867.                tmp_tiles_left--;
  868.  
  869.         /* all flowers and all seasons */
  870.  
  871.         board[j]->value = (i >= 34) ? ((i >= 38) ? 35 : 34) : i;
  872.         switch(i) {
  873.  
  874.         case 0: board[j]->image = (BandW) ? &DOT1 : &cDOT1;
  875.             break;
  876.         case 1: board[j]->image = (BandW) ? &DOT2 : &cDOT2;
  877.             break;
  878.         case 2: board[j]->image = (BandW) ? &DOT3 : &cDOT3;
  879.             break;
  880.         case 3: board[j]->image = (BandW) ? &DOT4 : &cDOT4;
  881.             break;
  882.         case 4: board[j]->image = (BandW) ? &DOT5 : &cDOT5;
  883.             break;
  884.         case 5: board[j]->image = (BandW) ? &DOT6 : &cDOT6;
  885.             break;
  886.         case 6: board[j]->image = (BandW) ? &DOT7 : &cDOT7;
  887.             break;
  888.         case 7: board[j]->image = (BandW) ? &DOT8 : &cDOT8;
  889.             break;
  890.         case 8: board[j]->image = (BandW) ? &DOT9 : &cDOT9;
  891.             break;
  892.         case 9: board[j]->image = (BandW) ? &BAM1 : &cBAM1;
  893.             break;
  894.         case 10: board[j]->image = (BandW) ? &BAM2 : &cBAM2;
  895.             break;
  896.         case 11: board[j]->image = (BandW) ? &BAM3 : &cBAM3;
  897.             break;
  898.         case 12: board[j]->image = (BandW) ? &BAM4 : &cBAM4;
  899.             break;
  900.         case 13: board[j]->image = (BandW) ? &BAM5 : &cBAM5;
  901.             break;
  902.         case 14: board[j]->image = (BandW) ? &BAM6 : &cBAM6;
  903.             break;
  904.         case 15: board[j]->image = (BandW) ? &BAM7 : &cBAM7;
  905.             break;
  906.         case 16: board[j]->image = (BandW) ? &BAM8 : &cBAM8;
  907.             break;
  908.         case 17: board[j]->image = (BandW) ? &BAM9 : &cBAM9;
  909.             break;
  910.         case 18: board[j]->image = (BandW) ? &CHA1 : &cCHA1;
  911.             break;
  912.         case 19: board[j]->image = (BandW) ? &CHA2 : &cCHA2;
  913.             break;
  914.         case 20: board[j]->image = (BandW) ? &CHA3 : &cCHA3;
  915.             break;
  916.         case 21: board[j]->image = (BandW) ? &CHA4 : &cCHA4;
  917.             break;
  918.         case 22: board[j]->image = (BandW) ? &CHA5 : &cCHA5;
  919.             break;
  920.         case 23: board[j]->image = (BandW) ? &CHA6 : &cCHA6;
  921.             break;
  922.         case 24: board[j]->image = (BandW) ? &CHA7 : &cCHA7;
  923.             break;
  924.         case 25: board[j]->image = (BandW) ? &CHA8 : &cCHA8;
  925.             break;
  926.         case 26: board[j]->image = (BandW) ? &CHA9 : &cCHA9;
  927.             break;
  928.         case 27: board[j]->image = (BandW) ? &GRED : &cGRED;
  929.             break;
  930.         case 28: board[j]->image = (BandW) ? &REDD : &cREDD;
  931.             break;
  932.         case 29: board[j]->image = (BandW) ? &WHTD : &cWHTD;
  933.             break;
  934.         case 30: board[j]->image = (BandW) ? &EAST : &cEAST;
  935.             break;
  936.         case 31: board[j]->image = (BandW) ? &WEST : &cWEST;
  937.             break;
  938.         case 32: board[j]->image = (BandW) ? &SOUT : &cSOUT;
  939.             break;
  940.         case 33: board[j]->image = (BandW) ? &NORT : &cNORT;
  941.             break;
  942.         case 34: board[j]->image = (BandW) ? &AUT : &cAUT;
  943.             break;
  944.         case 35: board[j]->image = (BandW) ? &SUM : &cSUM;
  945.             break;
  946.         case 36: board[j]->image = (BandW) ? &SPR : &cSPR;
  947.             break;
  948.         case 37: board[j]->image = (BandW) ? &WIN : &cWIN;
  949.             break;
  950.         case 38: board[j]->image = (BandW) ? &ORC : &cORC;
  951.             break;
  952.         case 39: board[j]->image = (BandW) ? &MUM : &cMUM;
  953.             break;
  954.         case 40: board[j]->image = (BandW) ? &BAM : &cBAM;
  955.             break;
  956.         case 41: board[j]->image = (BandW) ? &PLM : &cPLM;
  957.             break;
  958.         }
  959.  
  960.     }
  961.     /* establish default values */
  962.  
  963.         board[j]->left_free     = FALSE;
  964.         board[j]->right_free    = FALSE;
  965.         board[j]->top_free      = TRUE;
  966.         board[j]->left_next[0]  = j - 1;
  967.         board[j]->left_next[1]  = 999;
  968.         board[j]->right_next[0] = j + 1;
  969.         board[j]->right_next[1] = 999;
  970.         board[j]->covered[0]    = 999;
  971.         board[j]->covered[1]    = 999;
  972.         board[j]->covered[2]    = 999;
  973.         board[j]->covered[3]    = 999;
  974.         board[j]->removed       = FALSE;
  975.  
  976.     /* setup special cases */
  977.  
  978.         switch (j) {
  979.         case 139:
  980.         case 141: 
  981.             board[j]->top_free = FALSE;
  982.         case 0:
  983.         case 12:
  984.         case 20:
  985.         case 30:
  986.         case 57:
  987.         case 67:
  988.         case 75:
  989.         case 87:
  990.         case 93:
  991.         case 99:
  992.         case 105:
  993.         case 111:
  994.         case 117:
  995.         case 123:
  996.         case 127:
  997.         case 131:
  998.         case 135:
  999.             board[j]->left_free = TRUE;
  1000.             board[j]->left_next[0] = 999;
  1001.             break;
  1002.         case 140:
  1003.         case 142:
  1004.             board[j]->top_free = FALSE;
  1005.         case 11:
  1006.         case 19:
  1007.         case 29:
  1008.         case 56:
  1009.         case 66:
  1010.         case 74:
  1011.         case 86:
  1012.         case 92:
  1013.         case 98:
  1014.         case 104:
  1015.         case 110:
  1016.         case 116:
  1017.         case 122:
  1018.         case 126:
  1019.         case 130:
  1020.         case 134:
  1021.         case 138:
  1022.             board[j]->right_free = TRUE;
  1023.             board[j]->right_next[0] = 999;
  1024.             break;
  1025.         case 143:
  1026.             board[j]->right_free = TRUE;
  1027.             board[j]->left_next[0] = 999;
  1028.             board[j]->left_free = TRUE;
  1029.             board[j]->right_next[0] = 999;
  1030.             board[j]->covered[0] = 139;
  1031.             board[j]->covered[1] = 140;
  1032.             board[j]->covered[2] = 141;
  1033.             board[j]->covered[3] = 142;
  1034.             break;
  1035.         case 42:
  1036.             board[j]->right_next[0] = 55;
  1037.             break;
  1038.         case 43:
  1039.             board[j]->left_next[0] = 30;
  1040.             break;
  1041.         case 55:
  1042.             board[j]->left_next[1] = 42;
  1043.             break;
  1044.         }
  1045.             
  1046.     }
  1047.  
  1048.     /* special case (did not fit in above) */
  1049.  
  1050.     board[30]->right_next[1] = 43;
  1051.  
  1052.     /* set top_free flags  and covered pointers */
  1053.  
  1054.     for(i = 87, j = 13; i < 143; i++, j++) {
  1055.         board[i]->covered[0] = j;
  1056.         board[j]->top_free = FALSE;
  1057.         switch(j) {
  1058.             case 97:
  1059.             case 103:
  1060.             case 109:
  1061.             case 129:
  1062.                  j += 2;
  1063.                  break;
  1064.             case 18:
  1065.             case 64:
  1066.                  j += 3;
  1067.                  break;
  1068.             case 27:
  1069.             case 39:
  1070.                  j += 6;
  1071.                  break;
  1072.             case 51:
  1073.                  j += 7;
  1074.                  break;
  1075.             case 73:
  1076.                  j += 20;
  1077.                  break;
  1078.             case 115:
  1079.                  j += 12;
  1080.                  break;
  1081.         }
  1082.     }
  1083. }
  1084.  
  1085. /* This is the routine that returns the colored button image */
  1086.  
  1087. Pixrect *color_button(pr,color)
  1088.   struct pixrect *pr;
  1089.   int color;
  1090. {
  1091.   struct pixrect *color_pr;
  1092.  
  1093.   if(pr == NULL)
  1094.         return(NULL);
  1095.  
  1096.   /* if running in b/w mode return same pixrect */
  1097.   if (BandW)
  1098.         return(pr);
  1099.  
  1100.   /* make new pixrect */
  1101.   color_pr = mem_create(pr->pr_size.x, pr->pr_size.y, 8);
  1102.  
  1103.   /* copy pr to color_pr with color added */
  1104.   pr_rop(color_pr, 0, 0, pr->pr_size.x, pr->pr_size.y,
  1105.         PIX_SRC | PIX_COLOR( color ),
  1106.         pr,0,0);
  1107.  
  1108.   return(color_pr);
  1109. }
  1110.